package com.easydev.basic.web.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.easydev.basic.common.exception.DemoException;
import com.easydev.basic.service.DemoService;

@Controller
@RequestMapping(value = "/demo")
public class DemoController {

    private Logger log = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private DemoService demoService;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(HttpServletRequest request, HttpServletResponse response, ModelMap map) throws Exception {
        log.info("DemoController index执行");
        String excp = StringUtils.defaultIfBlank(request.getParameter("excp"), "");
        if (StringUtils.isNotBlank(excp)) {
            // 异常处理功能测试
            throw new DemoException("异常测试");
        }

        map.put("demoParam", StringUtils.defaultIfBlank(request.getParameter("param"), ""));
        map.put("time", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        // 修改orm-content.xml中的数据库配置，并修改对应表结构和字段后再打开，否则会报错
        // map.put("demoPojo", demoService.getDemoPojo(1));
        return "demo/index";
    }

}
